package com.annoncesautomobile.vue;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.annoncesautomobile.R;
import com.annoncesautomobile.modele.concreteEntities.Annonce;
import com.annoncesautomobile.modele.local.MesFavoris;
import com.annoncesautomobile.modele.util.IModelXML;
import com.annoncesautomobile.modele.util.ModelXML;
import com.annoncesautomobile.vue.actualites.ActualitesActivity;
import com.annoncesautomobile.vue.adapters.DernieresAnnoncesAdapter;
import com.annoncesautomobile.vue.adapters.ListeBoutonBas;
import com.annoncesautomobile.vue.affichageannonces.AffichageDetailAnnonceActivity;
import com.annoncesautomobile.vue.alertes.AlertesActivity;
import com.annoncesautomobile.vue.annuairepro.AnnuaireProChoixDepartementActivity;
import com.annoncesautomobile.vue.autotheque.AutothequeActivity;
import com.annoncesautomobile.vue.autres.AutresActivity;
import com.annoncesautomobile.vue.autres.MesAlertesActivity;
import com.annoncesautomobile.vue.autres.MesFavorisActivity;
import com.annoncesautomobile.vue.rechercheAnnonce.RechercheActivity;
import com.annoncesautomobile.vue.vendre.VendreActivity;

public class MainActivity extends SuperActivity {

	private final String EXTRA_IDANNONCE = "idAnnonce";

	private IModelXML model;
	private Context context;
	private List<Annonce> listeAnnonces;

	private HorizontalScrollView scrollDernieresAnnonces;
	private LinearLayout layoutScrollDernieresAnnonces;
	private ImageView buttonArrowLeft;
	private ImageView buttonArrowRight;

	private DernieresAnnoncesAdapter adapter;

	private boolean isFromNotif = false;

	public static final String EXTRA_NOM_INTENT 	= "isFromBoutonBas";
	public static final int INTENT_ANNONCE 			= 10;
	public static final int INTENT_VENDRE 			= 11;
	public static final int INTENT_ALERTE 			= 12;
	public static final int INTENT_ACTUALITE 		= 13;
	public static final int INTENT_AUTRE 			= 14;


	@Override
	protected void onNewIntent(Intent newIntent) {
		System.out.println("onResume Main");
		this.setIntent(newIntent);
	}

	@Override 
	protected void onPostResume()
	{
		super.onPostResume();
		this.isFromNotif = getIntent().getBooleanExtra("fromNotif",false);

		if(isFromNotif)
		{
			isFromNotif = false;
			getIntent().removeExtra("fromNotif");
			Intent i = new Intent(context, MesAlertesActivity.class);
			//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
		}


		int isFromBoutonBas = getIntent().getIntExtra(EXTRA_NOM_INTENT,-1);
		if(isFromBoutonBas != -1)
		{
			overridePendingTransition(0, 0);
			switch(isFromBoutonBas)
			{
			case INTENT_ACTUALITE : 
			{
				ListeBoutonBas.indiceButtonSelected = 3;
				Intent i = new Intent(MainActivity.this, ActualitesActivity.class);
				startActivity(i);

				break;
			}
			case INTENT_ALERTE : 
			{
				ListeBoutonBas.indiceButtonSelected = 2;
				Intent i = new Intent(MainActivity.this, AlertesActivity.class);
				startActivity(i);
				overridePendingTransition(0, 0);
				break;
			}
			case INTENT_AUTRE : 
			{
				ListeBoutonBas.indiceButtonSelected = 4;
				Intent i = new Intent(MainActivity.this, AutresActivity.class);
				startActivity(i);
				overridePendingTransition(0, 0);
				break;
			}
			case INTENT_VENDRE : 
			{
				ListeBoutonBas.indiceButtonSelected = 1;
				Intent i = new Intent(MainActivity.this, VendreActivity.class);
				startActivity(i);
				overridePendingTransition(0, 0);
				break;
			}
			}
		}
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		ListeBoutonBas.indiceButtonSelected = 0;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.context = this;
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		ListeBoutonBas.indiceButtonSelected = 0;
		setContentView(R.layout.activity_main);
		this.isFromNotif = getIntent().getBooleanExtra("fromNotif",false);

		ListeBoutonBas listeBoutonBas = new ListeBoutonBas(this);
		LinearLayout lt = (LinearLayout) this.findViewById(R.id.mainLayout);
		lt.addView(listeBoutonBas.getView());

		this.model = ModelXML.getInstance();
		this.listeAnnonces = this.model.getDernieresAnnonces();
		this.adapter = new DernieresAnnoncesAdapter(this, this.listeAnnonces);
		this.scrollDernieresAnnonces = (HorizontalScrollView) findViewById(R.id.scrollDernieresAnnonces);

		
		/*
		this.scrollDernieresAnnonces.setOnTouchListener(new OnTouchListener() {

			private Handler mHandler;
			private long mInitialDelay = 3000;
			private long mRepeatDelay = 1000;

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					if (mHandler != null)
						return true;
					mHandler = new Handler();
					mHandler.postDelayed(mAction, mInitialDelay);
					break;
				case MotionEvent.ACTION_UP:
					if (mHandler == null)
						return true;
					mHandler.removeCallbacks(mAction);
					mHandler = null;
					break;
				}
				return false;
			}

			Runnable mAction = new Runnable() {
				@Override
				public void run() {
					scrollDernieresAnnonces.scrollTo((int) scrollDernieresAnnonces.getScrollX() + 100, (int) scrollDernieresAnnonces.getScrollY());
					mHandler.postDelayed(mAction, mRepeatDelay);
				}
			};
		});
		*/


		this.layoutScrollDernieresAnnonces = (LinearLayout) findViewById(R.id.layoutScrollDernieresAnnonces);

		View viewPremierItem = (RelativeLayout) findViewById(R.id.layoutPremierItem);
		LinearLayout.LayoutParams paramsViewPremierItem = (LinearLayout.LayoutParams) viewPremierItem.getLayoutParams();
		int heightViewPremierItem = paramsViewPremierItem.height;

		for (int i = 0; i < this.adapter.getCount(); i++) {
			View item = this.adapter.getView(i, null, null);

			item.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Annonce annonce = (Annonce) adapter.getAnnonceByView(v);
					Intent intent = new Intent(MainActivity.this, AffichageDetailAnnonceActivity.class);
					intent.putExtra(EXTRA_IDANNONCE, Long.valueOf(Integer.valueOf(annonce.getIdAnnonces())));
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
				}
			});
			this.layoutScrollDernieresAnnonces.addView(item);

			LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) item.getLayoutParams();
			params.height = heightViewPremierItem;
			item.setLayoutParams(params);

		}
		this.layoutScrollDernieresAnnonces.removeView(viewPremierItem);
		
		
		this.buttonArrowLeft = (ImageView) findViewById(R.id.arrowLeftDernieresAnnonces);
		/*
		this.buttonArrowLeft.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				scrollDernieresAnnonces.scrollTo((int)scrollDernieresAnnonces.getScrollX() - 10, (int)scrollDernieresAnnonces.getScrollY());
			}
		});
		*/
		this.buttonArrowRight = (ImageView) findViewById(R.id.arrowRightDernieresAnnonces);
		/*
		this.buttonArrowRight.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				scrollDernieresAnnonces.scrollTo((int)scrollDernieresAnnonces.getScrollX() + 10, (int)scrollDernieresAnnonces.getScrollY());
			}
		});
		*/
	}

	@Override
	public void onBackPressed() {
		final Dialog dialog = new Dialog(MainActivity.this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.modal_go_nogo);
		final TextView text = (TextView) dialog.findViewById(R.id.text);
		text.setText("Quitter l'application ?");

		Button buttonNogo = (Button) dialog.findViewById(R.id.buttonNogo);
		buttonNogo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		Button buttonGo = (Button) dialog.findViewById(R.id.buttonGo);
		buttonGo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				MainActivity.super.onBackPressed();
			}
		});

		dialog.show();

	}


	public void clickRecherche(View view) {
		Intent myIntent = new Intent(view.getContext(), RechercheActivity.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivityForResult(myIntent, 0);
		overridePendingTransition(R.anim.aun, R.anim.adeux);
	}

	public void clickAutotheque(View view) {
		Intent myIntent = new Intent(view.getContext(), AutothequeActivity.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivityForResult(myIntent,0);
		overridePendingTransition(R.anim.aun, R.anim.adeux);
	}

	public void clickAnnuairePro(View view) {
		Intent myIntent = new Intent(view.getContext(), AnnuaireProChoixDepartementActivity.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivityForResult(myIntent,0);
		overridePendingTransition(R.anim.aun, R.anim.adeux);
	}

	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data) 
	{
		overridePendingTransition(R.anim.animationleftright, R.anim.animationrightleft);
	}
}
